<?php

namespace Admin\Controller;

class AccountController extends AdminController {

    public function index(){
        $where = " 1 ";
        $url = '';

        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }

        //订单状态搜索
        if(I('get.tab') >= 0 && I('get.tab') != ''){
            $filter['status'] = (int)I('get.tab');
            $url .= '&status=' . $filter['status'];
            $where .= " AND order_form.status =  " . $filter['status'];
        }else{
            $filter['status'] = '';
        }

        $limit = 8;
        $start = ($page - 1) * $limit;

        $model = M('orderlog');
        $data = $model->join('LEFT JOIN order_form of ON of.order_sn = orderlog.order_sn')
                      ->join('LEFT JOIN requirement re ON re.order_sn = orderlog.order_sn')
            ->where($where)->order('orderlog.id desc')
            ->limit($start,$limit)
            ->field('orderlog.order_sn,orderlog.alipay_sn,re.teacher_id,re.name as studentname,of.user_id,orderlog.eventtime,orderlog.coursenum,orderlog.coursenum_c')->select();
        foreach($data as $k=>$v){
            $userteacher = M('user_teacher');
            $map['user_id'] = $v['teacher_id'];
            $result = $userteacher->where($map)->find();
            $data[$k]['teachername']=$result['relname'];
        }
        foreach($data as $k=>$v){
            $user = M('user');
            $map1['id'] = $v['teacher_id'];
            $result = $user->where($map1)->field('phone')->find();
            //echo M('user_teacher')->getLastSql();die();
            $data[$k]['teacherphone']=$result['phone'];
        }
        foreach($data as $k=>$v){
            $user = M('user');
            $map2['id'] = $v['user_id'];
            $result = $user->where($map2)->field('phone')->find();
            $data[$k]['studentphone']=$result['phone'];
        }
        $total = $model//->join('LEFT JOIN user u ON u.id = order_form.user_id')
            ->where($where)->count();

        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Account/index') . '?page={page}' . $url;
        $render = $pagination->render();

        $new_url = U('Account/index') . '?page={page}' . $url;

        $this->assign('tab',I('get.tab'));
        $this->assign('lists',$data);
        $this->assign('nav','account');
        $this->assign('render',$render);
        $this->assign('url',$new_url);
        $this->assign('filter',$filter);

        $this->display("index");
    }

    public function recharge(){
        $where = " 1 and orderlog.status='TRADE_SUCC'";
        $url = '';

        //页码
        if(I('get.page')){
            $page = I('get.page');
        }else{
            $page = 1;
        }

        //订单状态搜索
        if(I('get.tab') >= 0 && I('get.tab') != ''){
            $filter['status'] = (int)I('get.tab');
            $url .= '&status=' . $filter['status'];
            $where .= " AND order_form.status =  " . $filter['status'];
        }else{
            $filter['status'] = '';
        }

        $limit = 8;
        $start = ($page - 1) * $limit;

        $model = M('orderlog');
        $data = $model->join('LEFT JOIN order_form of ON of.order_sn = orderlog.order_sn')
            ->join('LEFT JOIN requirement re ON re.order_sn = orderlog.order_sn')
            ->where($where)->order('orderlog.id desc')
            ->limit($start,$limit)
            ->field('orderlog.*,re.name as studentname,of.user_id')->select();
        foreach($data as $k=>$v){
            $user = M('user');
            $map2['id'] = $v['user_id'];
            $result = $user->where($map2)->field('phone')->find();
            $data[$k]['studentphone']=$result['phone'];
        }
        $total = $model->where($where)->count();

        $pagination = new \Pub\Controller\PaginationController();
        $pagination->total = $total;
        $pagination->page = $page;
        $pagination->limit = $limit;
        $pagination->url = U('Account/recharge') . '?page={page}' . $url;
        $render = $pagination->render();

        $new_url = U('Account/recharge') . '?page={page}' . $url;

        $this->assign('tab',I('get.tab'));
        $this->assign('lists',$data);
        $this->assign('render',$render);
        $this->assign('url',$new_url);
        $this->assign('filter',$filter);

        $this->display("recharge");
    }

    public function courseprice(){
        $course_price = M('course_price');
        $res = $course_price->select();
        $this->assign('lists',$res);
        //var_dump($res);
        $this->display("courseprice");
    }

    public function changeprice(){
        $is_certified = I("post.is_certified");
        $grade = I("post.grade");
        $type = I("post.type");
        $course_price = M('course_price');
        $map['is_certified'] = $is_certified;
        $map['grade'] = $grade;
        if($type==1){
            $data['price'] = I("post.price");
        }elseif($type==2){
            $data['teachprice'] = I("post.price");
        }
        $rs = $course_price->where($map)->save($data);
        if($rs){
            $data['error'] = 0;
            $data['msg'] = "修改价格成功";
        }else{
            $data['error'] = 1;
            $data['msg'] = "修改价格失败";
        }
        echo json_encode($data);
    }
}
